package cn.sx.service;

import cn.sx.config.ExceptionInterceptor;
import cn.sx.dao.ItemsDao;
import cn.sx.dao.OrderDao;
import cn.sx.entity.Carts;
import cn.sx.entity.Items;
import cn.sx.entity.Orders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc:订单服务
 *@Time: 2020/10/12  11:07
 */
@Service
public class OrdersService {
    @Resource
    private CartService cartService;
    @Resource
    private OrderDao orderDao;
    @Resource
    private GoodsService goodsService;
    @Resource
    private ItemsDao itemsDao;
    @Resource
    private UserService userService;
    /*保存订单*/
    @Transactional
    public int save(int userId) throws ExceptionInterceptor.MyException {
        List<Carts> cartsList = cartService.getList(userId);
        if (Objects.nonNull(cartsList) || cartsList.isEmpty()){
//            throw new ExceptionInterceptor.MyException("购物车没有商品");
        }
        /*验证库存*/
        for(Carts cart:cartsList){
            if (cart.getGood().getStock()<cart.getAmount()){
                throw new ExceptionInterceptor.MyException("商品["+cart.getGood().getName()+"]库存不足！");
            }
            goodsService.updateStock(cart.getGood().getId(),cart.getAmount());//减少库存
            goodsService.updateSales(cart.getGood().getId(),cart.getAmount());//增加销量
        }
        int total=0;
        for (Carts carts:cartsList){
            total += carts.getGood().getPrice() * carts.getAmount();
        }
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setTotal(total);
        orders.setAmount(cartsList.size());
        orders.setStatus(Orders.STATUS_UNPAY);
        orders.setSystime(new Date());
        orderDao.insert(orders);
        int orderId=orders.getId();
        for (Carts carts:cartsList){
            Items items = new Items();
            items.setOrderId(orderId);
            items.setGoodId(carts.getGoodId());
            items.setPrice(carts.getGood().getPrice());
            items.setAmount(carts.getAmount());
            itemsDao.insert(items);
        }
        //清空购物车
        cartService.clean(userId);
        return orderId;
    }

    public List<Orders> getList(byte status,int page,int size){
        return pack(status>0 ? orderDao.selectListByStatus
                (status,size*(page-1),size) : orderDao.selectList(page,size));
    }




    /*获取某用户全部订单*/
    public List<Orders> getListByUseid(int userId,int page,int size) {
        return pack(orderDao.selestListByUserid(userId,size*(page-1),size));
    }
    /*通过id获取*/

    public Orders get(int id) {
        return pack(orderDao.select(id));
    }

    private List<Orders>pack(List<Orders> list){
        if (Objects.nonNull(list) && !list.isEmpty()){
            for (Orders orders:list) {
                orders=pack(orders);
            }
        }
        return list;
    }

    private Orders pack(Orders orders) {
        if(Objects.nonNull(orders)){
            List<Items> itemList = itemsDao.selectList(orders.getId());
            for(Items items : itemList){
                items.setGood(goodsService.get(items.getGoodId()));
            }
            orders.setItemList(itemList);
            orders.setUser(userService.get(orders.getUserId()));
        }
        return orders;
    }
/*订单支付*/
    public void pay(Orders orders) {
            Orders old = orderDao.select(orders . getId());
            old.setStatus(Orders.STATUS_PAYED) ;
            old.setPaytype(orders.getPaytype());
            old.setName (orders.getName());
            old.setPhone(orders.getPhone());
            old.setAddress (orders.getAddress());
            orderDao.update (old);

    }

    /*获取总数*/
    public int getCountByUserid(int userId) {
        return orderDao.selectCountByUserid(userId);
    }


    public long getCount(byte status) {
        return status>0 ? orderDao.selectCountByStatus(status) : orderDao.selectCount();
    }

    public int delete(int id) {
        return orderDao.delete(id);
    }

    public boolean send(int id) {
        return orderDao.updateStatus(id,Orders.STATUS_SEND);
    }

    public boolean finsh(int id) {
        return orderDao.updateStatus(id,Orders.STATUS_FINISH);
    }


}
